package io.renren.modules.generator.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.controller.BaseController;
import io.renren.modules.generator.controller.vo.EnterpriseSupervisionDealVo;
import io.renren.modules.generator.controller.vo.EnterpriseSupervisionReportVo;
import io.renren.modules.generator.controller.vo.EnterpriseSupervisionVerifyVo;
import io.renren.modules.generator.entity.EnterpriseMainEntity;
import io.renren.modules.generator.entity.EnterpriseSupervisionFlowEntity;
import io.renren.modules.generator.entity.EnterpriseSupervisionRecordInfoEntity;
import io.renren.modules.generator.enums.RoleTypeEnum;
import io.renren.modules.generator.service.EnterpriseMainService;
import io.renren.modules.generator.service.EnterpriseSupervisionRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;


/**
 * 企业监管记录表
 *
 * @author zd
 * @date 2020-09-21 22:05:44
 */
@Validated
@RestController
@RequestMapping("generator/inspection")
@Api("企业监管记录表接口")
public class EnterpriseSupervisionRecordController extends BaseController {

    @Autowired
    private EnterpriseSupervisionRecordService enterpriseSupervisionRecordService;
    @Autowired
    private EnterpriseMainService enterpriseMainService;

    /**
     * 巡检上报
     */
    @PostMapping("/report")
    @ApiOperation("巡检上报")
    public R report(@Valid @RequestBody EnterpriseSupervisionReportVo vo) {
        if (!validateUser()) {
            return R.error("当前用户非安全员，网格员，安全专家角色！无操作权限。");
        }
        if (getDeptId() == null) {
            return R.error("用户无所属企业！无操作权限。");
        }
        EnterpriseMainEntity enterpriseMainEntity = enterpriseMainService.getByDeptid(Integer.parseInt(getDeptId()));
        if (RoleTypeEnum.message(getRoleCode()) == 1 && !StringUtils.equals(enterpriseMainEntity.getId().toString(),vo.getCode())) {
            return R.error("安全员只能上报自己所属企业！无操作权限。");
        }
        vo.setUserNo(getUserNo());
        vo.setUserName(getUserName());
        vo.setRoleType(RoleTypeEnum.message(getRoleCode()));
        enterpriseSupervisionRecordService.report(vo);
        return R.ok();
    }

    /**
     * 列表
     */
    @GetMapping("/page")
    @ApiOperation("巡检列表")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "query", name = "type", required = true,value = "类型(1:待处理，2：处理中，3：已完结)", dataType = "Integer"),
        @ApiImplicitParam(paramType = "query", name = "checkType", required = true,value = "检查类型 (1:安全巡检,2:环保巡检)", dataType = "Integer"),
        @ApiImplicitParam(paramType = "query", name = "keyword", required = true,value = "搜索", dataType = "String")
    })
    public R queryPage(Map<String, Object> params,int type,int checkType,String keyword) {
        if (!validateUser()) {
            return R.error("当前用户非安全员，网格员，安全专家角色！无操作权限。");
        }
        params.put("userNo",getUserNo());
        params.put("userName", getUserName());
        params.put("deptId", getDeptId());
        PageUtils page = enterpriseSupervisionRecordService.queryPage(params,RoleTypeEnum.message(getRoleCode()),type,checkType,keyword);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @GetMapping("/info")
    @ApiOperation("根据id获取实体")
    public R info(String id) {
        if (!validateUser()) {
            return R.error("当前用户非安全员，网格员，安全专家角色！无操作权限。");
        }
        EnterpriseSupervisionRecordInfoEntity enterpriseSupervisionRecordInfoEntity = enterpriseSupervisionRecordService.info(id);
        if (!CollectionUtils.isEmpty(enterpriseSupervisionRecordInfoEntity.getFlowList())) {
            for (EnterpriseSupervisionFlowEntity enterpriseSupervisionFlowEntity : enterpriseSupervisionRecordInfoEntity.getFlowList()) {
                if (enterpriseSupervisionFlowEntity.getRemark() == null) {
                    enterpriseSupervisionFlowEntity.setRemark("");
                }
            }
        }
        return R.ok().put("model", enterpriseSupervisionRecordInfoEntity);
    }

    /**
     * 安全员处理
     */
    @PostMapping("/deal")
    @ApiOperation("安全员处理")
    public R deal(@Valid @RequestBody EnterpriseSupervisionDealVo vo) {
        if (RoleTypeEnum.message(getRoleCode()) != 1) {
            return R.error("当前用户非安全员！无操作权限。");
        }
        vo.setUserNo(getUserNo());
        vo.setUserName(getUserName());
        vo.setRoleType(RoleTypeEnum.message(getRoleCode()));
        enterpriseSupervisionRecordService.deal(vo);
        return R.ok();
    }

    /**
     * 网格员，安全专家核实
     */
    @PostMapping("/verify")
    @ApiOperation("网格员，安全专家核实")
    public R verify(@Valid @RequestBody EnterpriseSupervisionVerifyVo vo) {
        int roleType = RoleTypeEnum.message(getRoleCode());
        if (roleType != 2 && roleType != 3) {
            return R.error("当前用户非网格员，安全专家！无操作权限。");
        }
        vo.setUserNo(getUserNo());
        vo.setUserName(getUserName());
        vo.setRoleType(roleType);
        enterpriseSupervisionRecordService.verify(vo);
        return R.ok();
    }


    private boolean validateUser () {
        if (RoleTypeEnum.message(getRoleCode()) == -1) {
            return false;
        }
        return true;
    }
}
